﻿/* 
 *              长沙理工大学 软件1703 刘鑫
 *              @版权所有，翻版必究
 *              字符串生成器1.0版
 *              日期：2018年4月28日
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        private List<string> StringList;
        private List<string> tmpStringList;
        private Random random;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int seed = (int) DateTime.Now.Ticks;
            StringList =new List<string>();
            tmpStringList=new List<string>();
            random = new Random(seed);
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Pokémon天下第一!\n注意！使用了导出结果文件就不能使用生成随机数组对了！\n反之亦然，所以请想好自己的输出方式！", "Tip");
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            
        }

        private void AddBut_Click(object sender, EventArgs e)
        {
            if (ReadTextBox.Text == "")
            {
                MessageBox.Show("请在输入框输入数据或者导入文件！", "Tip");
                return;
            }
            string[] sr = ReadTextBox.Text.Split(new char[] {'\n', ' ','\r'});
            ReadTextBox.Text = "";
            for (int i = 0; i < sr.Length; i++)
            {
                if (sr[i]=="") continue;
                StringList.Add(sr[i]);
                tmpStringList.Add(sr[i]);
            }
        }

        private void ClearBut_Click(object sender, EventArgs e)
        {
            StringList.Clear();
            tmpStringList.Clear();
            MessageBox.Show("已将储存的输入数据清空！", "Tip");
        }

        private void InputBut_Click(object sender, EventArgs e)
        {
            DialogResult input= openFileDialog1.ShowDialog();
            if (input == DialogResult.OK)
            {
                string FileName = openFileDialog1.FileName;
                string text = File.ReadAllText(FileName, Encoding.GetEncoding("gb2312"));
                
                if (ReadTextBox.Text == "")
                {
                    ReadTextBox.Text = text;
                }
                else
                {
                    text = " " + text;
                    ReadTextBox.Text += text;
                }
            }
        }

        
        private List<string> MakeStringCombine()
        {
            List<string> List1= new List<string>(StringList);
            List<string> List2 = new List<string>(StringList);
            int len = StringList.Count;
            List<string>res=new List<string>();
            for (int i = 0; i <len; i++)
            {
                int first = random.Next(0, StringList.Count);
                int second = random.Next(0, StringList.Count);
                while (StringList[first] == tmpStringList[second])
                {
                    second = random.Next(0, StringList.Count);
                }
                string newSrting = "(" + StringList[first] + "," + tmpStringList[second] + ")";
                res.Add(newSrting);
                StringList.RemoveAt(first);
                tmpStringList.RemoveAt(second);
            }

            if (1 < len-1)
                len = random.Next(1, len-1);
            else len = 0;
            for (int i = 0; i < len; i++)
            {
                int first = random.Next(0, List1.Count);
                int second = random.Next(0, List1.Count);
                while (List1[first] == List2[second])
                {
                    second = random.Next(0, List1.Count);
                }
                string newSrting = "(" + List1[first] + "," + List2[second] + ")";
                res.Add(newSrting);
                List1.RemoveAt(first);
                List2.RemoveAt(second);
            }
            res.Sort();
            return res;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(StringList.Count==0)
            {
                MessageBox.Show("您未输入数据或者已经进行过输出了！", "Tip");
                return;
            }
            List<string> res=new List<string>();
            res = MakeStringCombine();
            ReadTextBox.Clear();
            for (int i = 0; i < res.Count; i++)
            {
                ReadTextBox.Text += res[i] + " ";
            }
        }

        private void OutputBut_Click(object sender, EventArgs e)
        {
            DialogResult output = openFileDialog1.ShowDialog();
            if (output == DialogResult.OK)
            {
                string FileName = openFileDialog1.FileName;
                FileStream nowFileStream = File.Open(FileName, FileMode.Create, FileAccess.Write);
                StreamWriter neWriter = new StreamWriter(nowFileStream, Encoding.GetEncoding("gb2312"));
                List<string> tmpList = MakeStringCombine();
                if (tmpList.Count == 0)
                {
                    MessageBox.Show("请在输入框输入数据或者导入文件！", "Tip");
                }
                for (int i = 0; i < tmpList.Count; i++)
                {
                   neWriter.WriteLine(tmpList[i]);
                }
                neWriter.Close();
                nowFileStream.Close();
                MessageBox.Show("已经成功导出字符串对！");
            }
        }
    }
}
